/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.controller.model;

import net.groupme.model.cache.Cachable;


/**
 * created on Jun 26, 2007
 * 
 * Instances of this class are exchanged between the actual view and the component that
 * renders resources...
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.2 $ $Date: 2007-07-23 18:18:19 $
 */
public class HtmlFormattedElement implements Cachable{
	
	/** generated UID  */
	private static final long serialVersionUID = -4670517983473014526L;

	/** seperator for string representation of divId attribute */
	private static final String CACHABLE_CONSTANT_BEGIN_DIV_ID = "<hfe:div>";
	
	/** seperator for string representation of divId attribute */
	private static final String CACHABLE_CONSTANT_END_DIV_ID = "</hfe:div>";
	
	/** seperator for string representation of HTML attribute */
	private static final String CACHABLE_CONSTANT_BEGIN_HTML = "<hfe:html>";
	
	/** seperator for string representation of HTML attribute */
	private static final String CACHABLE_CONSTANT_END_HTML = "</hfe:html>";
	
	//attributes:
	private String divID = null;
	private String html = null;
	
	/**
	 * Creates a new empty HtmlFormattedElement instance. 
	 */
	public HtmlFormattedElement() {
		super();
	}
	/**
	 * Creates a new HtmlFormattedElement instance.
	 * @param divID the id of the div element which is embodied in the <i>html</i> attribute of this obkject
	 * @param html the actual HTML-formatted string 
	 */
	public HtmlFormattedElement(String divID, String html) {
		super();
		this.divID = divID;
		this.html = html;
	}
	/**
	 * This method returns the divID of a HtmlFormattedElement object.
	 * @return the divID
	 */
	public String getDivID() {
		return divID;
	}
	/**
	 * This method sets the divID of a HtmlFormattedElement object.
	 * @param divID the divID to set
	 */
	public void setDivID(String divID) {
		this.divID = divID;
	}
	/**
	 * This method returns the html of a HtmlFormattedElement object.
	 * @return the html
	 */
	public String getHtml() {
		return html;
	}
	/**
	 * This method sets the html of a HtmlFormattedElement object.
	 * @param html the html to set
	 */
	public void setHtml(String html) {
		this.html = html;
	}
	/* (non-Javadoc)
	 * @see net.groupme.model.cache.Cachable#convertToString()
	 */
	public String convertToString() {
		String asString = "";
		if(divID != null){
			asString += CACHABLE_CONSTANT_BEGIN_DIV_ID + divID + CACHABLE_CONSTANT_END_DIV_ID;
		}
		if(html != null){
			asString += CACHABLE_CONSTANT_BEGIN_HTML + html + CACHABLE_CONSTANT_END_HTML;
		}
		return asString;
	}
	/* (non-Javadoc)
	 * @see net.groupme.model.cache.Cachable#readFromString(java.lang.String)
	 */
	public void readFromString(String objectAsString) {
		if(objectAsString != null){
			if(objectAsString.contains(CACHABLE_CONSTANT_BEGIN_DIV_ID) &&
					objectAsString.contains(CACHABLE_CONSTANT_END_DIV_ID)){
				divID = objectAsString.substring(
							objectAsString.indexOf(CACHABLE_CONSTANT_BEGIN_DIV_ID) + CACHABLE_CONSTANT_BEGIN_DIV_ID.length(), 
							objectAsString.indexOf(CACHABLE_CONSTANT_END_DIV_ID));
			}
			if(objectAsString.contains(CACHABLE_CONSTANT_BEGIN_HTML) &&
					objectAsString.contains(CACHABLE_CONSTANT_END_HTML)){
				html = objectAsString.substring(
							objectAsString.indexOf(CACHABLE_CONSTANT_BEGIN_HTML) + CACHABLE_CONSTANT_BEGIN_HTML.length(), 
							objectAsString.indexOf(CACHABLE_CONSTANT_END_HTML));
			}
			
		}
	}
}
